//
//  GY_NewMarketCenterVC.m
//  GeYou
//
//  Created by MRzhang on 2018/3/15.
//  Copyright © 2018年 geyou. All rights reserved.
//

#import "GY_NewMarketCenterVC.h"
#import "Head.h"
#import "MarketCenterColView.h"
#import "GYMarketCenterListVC.h"
#import "GYHomeDealingVC.h"
#import "GYHomeMarketPriceVC.h"
#import "GYHomeWeb.h"

@interface GY_NewMarketCenterVC ()<SDCycleScrollViewDelegate>
{
    NSMutableArray* titleArr;
    NSArray* sourceArr;
}

@property (nonatomic, strong) UIScrollView *contentView;
@property (nonatomic, strong) NSMutableArray *PassArr;
@property (nonatomic, strong) NSMutableDictionary *topLine;  //头条
@property (nonatomic, strong) NSMutableDictionary *shot;     //人物
@property (nonatomic, copy) NSArray *bannerDatas;

@end

@implementation GY_NewMarketCenterVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"行情中心"];
    
    [self loadData];

    sourceArr=@[@[@{@"title":@"头条",@"img":@"mk_01"}],
                @[@{@"title":@"市场价格",@"img":@"mk_02"},@{@"title":@"皮革指数",@"img":@"mk_03"}],
                @[@{@"title":@"行业资讯",@"img":@"mk_04"},@{@"title":@"环球信息",@"img":@"mk_06"}],
                @[@{@"title":@"政策法规",@"img":@"mk_05"},@{@"title":@"人物专访",@"img":@"mk_07"}],
                ];
    self.baseTable.separatorStyle=UITableViewCellSeparatorStyleNone;

    self.baseTable.height=self.baseTable.height-GY_Nav_height;
    [self.baseTable.mj_footer setHidden:YES];
    [self.view addSubview:self.baseTable];
    // Do any additional setup after loading the view.
}

-(void)refresh
{
    [self loadData];
    [self.baseTable.mj_header endRefreshing];
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{

    if (cycleScrollView.tag==100)
    {

        NSDictionary *model = _bannerDatas[index];
        GYHomeWeb *contentCtr = [[GYHomeWeb alloc] init];
        contentCtr.information = @{@"url":[model objectForKey:@"url"]};
       // contentCtr.passTitle = @"头条";
        [self.navigationController pushViewController:contentCtr animated:YES];
    }else
    {
        GYHomeDealingVC *ctr = [[GYHomeDealingVC alloc] init];
        ctr.sourceArr=_PassArr;
        [self.navigationController pushViewController:ctr animated:YES];
    }
}


#pragma mark - UITableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0)
    {
        static NSString *CellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor whiteColor];
            
            SDCycleScrollView* scroll=[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) delegate:self placeholderImage:[UIImage imageNamed:@"default_goods"]];
            scroll.tag=100;
            scroll.pageDotColor=RGBACOLOR(208, 2, 27,1);
            scroll.bannerImageViewContentMode=UIViewContentModeScaleAspectFill;
            [cell.contentView addSubview:scroll];

            
            
            SDCycleScrollView* titlescroll=[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(20, 210, SCREEN_WIDTH-40, 25) delegate:self placeholderImage:[UIImage imageNamed:@""]];
            titlescroll.tag=101;
            titlescroll.onlyDisplayText=YES;
            [cell.contentView addSubview:titlescroll];
            titlescroll.backgroundColor=Main_Color_BGLightGray;
            
            
        }
        
        SDCycleScrollView* imageScrol=(SDCycleScrollView*)[cell.contentView viewWithTag:100];
        NSMutableArray* imgAry=[[NSMutableArray alloc] init];
        NSMutableArray* nameArr=[[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < _bannerDatas.count; i++) {
            NSDictionary *model = _bannerDatas[i];
            NSString *imgUrl = [NSString stringWithFormat:@"%@%@", model[@"imgurl"], kProductImgOriginSuffix];
            [imgAry addObject:imgUrl];
            [nameArr addObject:model[@"name"]];
        }
        imageScrol.pageControlAliment=SDCycleScrollViewPageContolAlimentRight;
        imageScrol.imageURLStringsGroup=imgAry;
        imageScrol.titlesGroup=nameArr;
        
        SDCycleScrollView* titleAcrol=(SDCycleScrollView*)[cell.contentView viewWithTag:101];
        titleAcrol.titlesGroup=titleArr;
        titleAcrol.titleLabelTextColor=Main_Color_Black;
        titleAcrol.titleLabelBackgroundColor=Main_Color_BGLightGray;
        titleAcrol.scrollDirection=UICollectionViewScrollDirectionVertical;
        titleAcrol.layer.cornerRadius = 25 / 2.0f;
        [titleAcrol.layer setMasksToBounds:YES];
        titleAcrol.titleLabelTextAlignment=NSTextAlignmentCenter;
        
        return cell;
    }
    
    static NSString *CellIdentifier = @"seccell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = Main_Color_BGLightGray;
        
    }else
    {
        for (UIView* view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
    }
    float ww=(SCREEN_WIDTH)/2-1;
     @WeakSelf;
    for (int i=0; i<[sourceArr count]; i++)
    {
        UIView* colView=[[UIView alloc] init];
        colView.frame=CGRectMake((ww+1)*(i%2), (ww+1)*(i/2), ww, ww);
        colView.backgroundColor=[UIColor whiteColor];
        colView.userInteractionEnabled=YES;
        [cell.contentView addSubview:colView];
        
        if (i!=0)
        {
            colView.backgroundColor=[UIColor clearColor];
            for (int j=0; j<2; j++)
            {
                MarketCenterColView* mm=[[MarketCenterColView alloc] init];
                mm.backgroundColor=[UIColor whiteColor];
                mm.frame=CGRectMake(0, (ww/2+0.5)*j, ww, ww/2-0.5);
                mm.sourceDic=[sourceArr[i] objectAtIndex:j];

                [colView addSubview:mm];

                if (i==1&&j==0)
                {
                    [mm addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                        GYHomeMarketPriceVC *vc = [[GYHomeMarketPriceVC alloc] init];
                        [self.navigationController pushViewController:vc animated:YES];
                    }];

                }

                if (i==1&&j==1)
                {
                    [mm addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                        GYMarketCenterListVC *commonCtr = [[GYMarketCenterListVC alloc] init];
                        commonCtr.navTitle = @"皮革指数";
                        commonCtr.type = @"3_8";
                        [selfp.navigationController pushViewController:commonCtr animated:YES];
                    }];
                }


                if (i==2&&j==0)
                {
                    [mm addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                        GYMarketCenterListVC *commonCtr = [[GYMarketCenterListVC alloc] init];
                        commonCtr.navTitle = @"行业资讯";
                        commonCtr.type = @"3_2";
                        [selfp.navigationController pushViewController:commonCtr animated:YES];
                    }];

                }

                if (i==2&&j==1)
                {
                    [mm addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                        GYMarketCenterListVC *commonCtr = [[GYMarketCenterListVC alloc] init];
                        commonCtr.navTitle = @"环球信息";
                        commonCtr.type = @"3_3";
                        [selfp.navigationController pushViewController:commonCtr animated:YES];
                    }];

                }

                if (i==3&&j==0)
                {
                    [mm addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                        GYMarketCenterListVC *commonCtr = [[GYMarketCenterListVC alloc] init];
                        commonCtr.navTitle = @"政策法规";
                        commonCtr.type = @"3_5";
                        [selfp.navigationController pushViewController:commonCtr animated:YES];
                    }];

                }

                if (i==3&&j==1)
                {

                    [mm addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                        GYHomeWeb *contentCtr = [[GYHomeWeb alloc] init];
                        contentCtr.information = @{@"url":[NSString stringWithFormat:@"%@%@",GYWebUrl,_shot[@"id"]]};
                        contentCtr.passTitle = @"人物专访";
                        [self.navigationController pushViewController:contentCtr animated:YES];
                    }];
                }

            }

        }else
        {
            MarketCenterColView* mm=[[MarketCenterColView alloc] init];
            mm.backgroundColor=[UIColor whiteColor];
            mm.frame=CGRectMake(0, 0, ww, ww);
            mm.issingle=YES;
            mm.sourceDic=[sourceArr[i] objectAtIndex:0];
            [colView addSubview:mm];
            
            [mm addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                GYHomeWeb *contentCtr = [[GYHomeWeb alloc] init];
                contentCtr.information = @{@"url":[NSString stringWithFormat:@"%@%@",GYWebUrl,[_topLine objectForKey:@"id"]]};
                contentCtr.passTitle = @"头条";
                [self.navigationController pushViewController:contentCtr animated:YES];
            }];

        }
    }

    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc]init];
    headView.backgroundColor = Main_Color_BGLightGray;
    return headView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc]init];
    footView.backgroundColor = [UIColor clearColor];
    return footView;
}
#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==1) {
        return 5;
    }
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {

        return 245;
    }
    return SCREEN_WIDTH;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{
    
}

-(void)loadData
{
    [self initAllData];
    [self requestbannerData];
    [self setAdvertData];
}
- (void)initAllData
{
    NSString *interface = @"newsAlone";
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc] init];
    [requestDic setObject:@"3_1" forKey:@"type"];
    
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:requestDic handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSDictionary *data = infoModel.data;
        if ([data isKindOfClass:[NSDictionary class]])
        {
            _topLine=[[NSMutableDictionary alloc] initWithDictionary:data];
        }
        
    }];
    
    //人物专访
    [requestDic setObject:@"3_6" forKey:@"type"];
    
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:requestDic handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSDictionary *data = infoModel.data;
        if ([data isKindOfClass:[NSDictionary class]])
        {
             _shot=[[NSMutableDictionary alloc] initWithDictionary:data];
        }
        
    }];

}

-(void)requestbannerData
{

    NSString *interface = @"homePageBanner";
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc] init];
    [requestDic setObject:@"2_D" forKey:@"type"];
    [requestDic setObject:[NSNumber numberWithInt:2] forKey:@"lx"];
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:requestDic handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSArray *data = infoModel.data;
        if ([data isKindOfClass:[NSArray class]])
        {
            _bannerDatas=[[NSMutableArray alloc] initWithArray:data];
            [self.baseTable reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
        }
    }];
}

- (void)setAdvertData
{
    titleArr=[[NSMutableArray alloc] init];
    
    NSString *interface = @"actualTimeDeal";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:nil handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSArray *data = infoModel.data;
        if ([data isKindOfClass:[NSArray class]])
        {
            _PassArr=[[NSMutableArray alloc] initWithArray:data];
            for (NSDictionary* dic in data)
            {
                [titleArr addObject:[NSString stringWithFormat:@"%@ %@ %@ %@",dic[@"region"],dic[@"protype"],dic[@"price"],dic[@"time"]]];
            }
            
            [self.baseTable reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
        }
    }];
}
@end

